clear
set more off
set matsize 5000

global lecmen    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\MENAGES"
global lecmensas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\MENAGES"
global ecrmen    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\MENAGES\autres_var"
global ecrstat   "\\intra\partages\au_amic2\SRCV_AVRIL2021\STAT_fev2022_V5\2004_2008"


/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/*     fichier STATA 1ère livraison 2008-2017 */
foreach an of numlist 4 / 7 {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep idmenc_* men hx060 nactifs  nadult nenfants hx080 hx090 nivact chargr rembpa remvar epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
	 gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 8 {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep idmenc_* men hx060 nactifs  nadult nenfants hx080 hx090 nivact chargr rembpa remvar ipcred ipelec ipimp iplog iploy  epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
	 gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'_bis.dta, replace
display `an'
count 
}

/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 4/7  {
cd "$ecrmen"
use autres_men_`an'.dta, clear
rename hx060 HX060
rename hx080 HX080
rename hx090 HX090
rename idmenc_* idmenc
save autres_men_`an'.dta, replace
}

foreach an of numlist 8  {
cd "$ecrmen"
use autres_men_`an'_bis.dta, clear
rename hx060 HX060
rename hx080 HX080
rename hx090 HX090
rename idmenc_* idmenc
save autres_men_`an'_bis.dta, replace
}

/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrmen\autres_men_8_bis.dta", clear
append using 	"$ecrmen\autres_men_4.dta" "$ecrmen\autres_men_5.dta" /// 
                "$ecrmen\autres_men_6.dta" "$ecrmen\autres_men_7.dta" 
order idmenc annee_SRCV men
sort idmenc annee_SRCV men				
save "$ecrmen\autres_menages_2004_2008.dta", replace

/* 4 - MISE EN FORME DE NIVACT_NEW / NIVACT_NEW2 / IPLOY_NEW / REMBPA */
/* a - MISE EN FORME DE NIVACT_NEW avec 6 modalités */
clear
use "$ecrmen\autres_menages_2004_2008.dta", clear
gen nivact_new = nivact if (annee_SRCV == 2005 | annee_SRCV == 2006 | annee_SRCV == 2007 | annee_SRCV == 2008)
sort annee_SRCV
order annee_SRCV nivact nivact_new
tab nivact annee_SRCV
tab nivact_new annee_SRCV
* 2005-2008
replace nivact_new = "6" if (nivact == "1" & annee_SRCV != 2004)
replace nivact_new = "5" if (nivact == "2" & annee_SRCV != 2004)
replace nivact_new = "4" if (nivact == "3" & annee_SRCV != 2004)
replace nivact_new = "3" if (nivact == "4" & annee_SRCV != 2004)
replace nivact_new = "2" if (nivact == "5" & annee_SRCV != 2004)
replace nivact_new = "1" if (nivact == "6" & annee_SRCV != 2004)
* 2004
replace nivact_new = "" if (annee_SRCV == 2004)
tab nivact annee_SRCV, missing
tab nivact_new annee_SRCV, missing
*drop nivact 
save "$ecrmen\autres_menages_2004_2008_def.dta", replace

/* b - MISE EN FORME DE NIVACT_NEW2 avec 5 modalités */
clear
use "$ecrmen\autres_menages_2004_2008_def.dta", clear
gen nivact_new_2 = " "
sort annee_SRCV
order annee_SRCV nivact_new nivact_new_2
* 2005-2008
replace nivact_new_2 = "1" if (nivact == "6" & annee!=2004 )
replace nivact_new_2 = "2" if (nivact == "5" & annee!=2004 )
replace nivact_new_2 = "3" if (nivact == "4" & annee!=2004 )
replace nivact_new_2 = "4" if (nivact == "3" & annee!=2004 )
replace nivact_new_2 = "5" if (nivact == "2" & annee!=2004 )
replace nivact_new_2 = "5" if (nivact == "1" & annee!=2004 )
* 2004
replace nivact_new_2 = "1" if (nivact == "5" & annee==2004 )
replace nivact_new_2 = "2" if (nivact == "4" & annee==2004 )
replace nivact_new_2 = "3" if (nivact == "3" & annee==2004 )
replace nivact_new_2 = "4" if (nivact == "2" & annee==2004 )
replace nivact_new_2 = "5" if (nivact == "1" & annee==2004 )
tab nivact annee_SRCV, missing
tab nivact_new annee_SRCV, missing
tab nivact_new_2 annee_SRCV, missing
*drop nivact
save "$ecrmen\autres_menages_2004_2008_def.dta", replace

/* c - MISE EN FORME DE IPLOY_NEW */
clear
use "$ecrmen\autres_menages_2004_2008_def.dta", clear
gen iploy_new = iploy 
sort annee_SRCV
order annee_SRCV iploy iploy_new
replace iploy_new = "4" if iploy == "4" | iploy == "" 
replace iploy_new = " " if annee_SRCV == 2004 
replace iploy_new = " " if annee_SRCV == 2005 
replace iploy_new = " " if annee_SRCV == 2006 
replace iploy_new = " " if annee_SRCV == 2007 
tab iploy annee_SRCV, missing
tab iploy_new annee_SRCV, missing
save "$ecrmen\autres_menages_2004_2008_def.dta", replace

/* d - REMBPA */
clear
use "$ecrmen\autres_menages_2004_2008_def.dta", clear
tab rembpa annee_SRCV, missing

gen rembpa_new = ""
sort annee_SRCV
order annee_SRCV rembpa rembpa_new
replace rembpa_new = "1" if rembpa == "1" & annee_SRCV == 2004 
replace rembpa_new = "2" if rembpa == "2" & annee_SRCV == 2004 
replace rembpa_new = "2" if rembpa == "3" & annee_SRCV == 2004 
replace rembpa_new = "2" if rembpa == "4" & annee_SRCV == 2004 
replace rembpa_new = "3" if rembpa == "5" & annee_SRCV == 2004 
replace rembpa_new = "3" if rembpa == "6" & annee_SRCV == 2004 
replace rembpa_new = "3" if rembpa == "7" & annee_SRCV == 2004 

tab rembpa_new rembpa, missing
tab rembpa annee_SRCV, missing
tab rembpa_new annee_SRCV, missing

gen rembpa_fin = ""
replace rembpa_fin = rembpa_new if annee_SRCV == 2004 
replace rembpa_fin = rembpa if annee_SRCV != 2004 

tab rembpa annee_SRCV, missing
tab rembpa_new annee_SRCV, missing
tab rembpa_fin annee_SRCV, missing
order annee_SRCV rembpa rembpa_new rembpa_fin 

drop rembpa rembpa_new
rename rembpa_fin rembpa
tab rembpa annee_SRCV, missing

save "$ecrmen\autres_menages_2004_2008_def.dta", replace

/* e - CREATION DE YEAR */
clear
use "$ecrmen\autres_menages_2004_2008_def.dta", clear
gen year = annee_SRCV - 1
save "$ecrmen\autres_menages_2004_2008_def.dta", replace


/* 5 - MISE EN FORME AVANT EMPILEMENT : TRANSFORMATION DES VARIABLES CARACTERE EN NUMERIQUE */
clear
use "$ecrmen\autres_menages_2004_2008_def.dta", clear

foreach var of varlist nivact_new nivact_new_2 chargr rembpa remvar ipcred ipelec ipimp iplog iploy iploy_new  epargn equil puisec stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval {
tab `var', missing
destring `var', replace
tab `var', missing
}
sort men annee_SRCV year
order men annee_SRCV idmenc year HX060 HX090 HX080 nadult nactifs nenfants nivact_new nivact_new_2 chargr rembpa remvar ///
      ipcred ipelec  ipimp  iplog iploy iploy_new epargn equil puisec ///
	  stoc mrfexo mrfliv mrfasv mrflog mrfval mrfaut
save "$ecrmen\autres_menages_2004_2008_def.dta", replace

/****************************************/
/* 13 - appariement agregats et ménages */
/****************************************/
/* 69 var + 30 var - 3 var (fusion)- 1 var (commune aux 2 tables = year) = 94 variables finales */

global ecrtot   "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\TOTAL"
global ecrmen   "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\MENAGES\autres_var"
global ecrprov  "\\intra\partages\au_amic2\SRCV_AVRIL2021\prov_FEV2022_V5"

clear
use "$ecrmen\autres_menages_2004_2008_def.dta"
clear
use "$ecrtot\ind_men_2004_2008_agregats.dta"
merge m:m idmenc men annee_SRCV using "$ecrmen\autres_menages_2004_2008_def.dta"
order men ind annee_SRCV nivact nivact_new nivact_new_2 iploy iploy_new
sort men ind annee_SRCV 
drop _merge
save "$ecrprov\recompo_menages.dta", replace




